package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102300742877";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8Ot/sasFdwElI5/Fm7OdT0YDrmMku7FKtB26PVm2Uq1if4g6IRw4+kLd3ALm/U0riPaldrfk/nmLYR4hTUP9an0PQMI/y6Ifmvvu3j9Ywbsx33ZJAp0uxf/1mkPBWCe3d+KohC7uX6TPst8IBEfAX+veFTrnKJDejszl7gXhlOl1i+KAppZIB1kusxEPzYHRENGl8pNUlT99Vapj7j1zjUJd+Y0+2+TZIdj73LAyqjmo32pvNuFYnmAWBCUQEr6+nVk8voSORQ8zGVS7esW5e+h8UDVc9NcnhVFLlHAYW0nI3+LQIslenvDulZZHJqF5vox8R5bCnodGH9Qyj71vpAgMBAAECggEAc5jUnCSTzDeiyhtnrYYgKuGnAwk5xI9t1sWRqIWzlnIEY2w48IhyvwmxImxXpqBIhNjJgratM9zZZ2nG1yQPLmBRDTJFtTAJBjST8g9rwyLIf3SbtQkI+2FXBf5NGwnOA1wLX3ANLSYStXXEt1OE17VQ/ZVzlCjZ4rbWvnqsrb00YBDpL9q03kRjAtHzhpiyol6d1JQhVAIxGv3WASmGr4ZHEW1raVex5TuaaZ/VLAfbR3dtcsmkmtlGPvQ/Tpnil2WAA+1vZC67KYivN9f0zkvlMUfVYEzSDF5aUePwXZGEQ5mUtxvnb+GlDQwhqWouQOu+2o122aaBCHVzyfIhgQKBgQDmIxvzVrZy3v4RpAPgMoQDmInTsxVs55YtDg8H02fXudKniq+yGbzGsBR/Yb38cJcoPJXPuFVzSSHUlaTjS6SyMCznwyuAQN/Kt3xyopl+ad7zupQoSgCpheduQRQWkYhNCGZJmL1/yhBkD5xCr8oVc5GPgJcHJExsk4lc+vEBNQKBgQDRYh/itK8UMJNSKJLbyVT6Sy5s9Sfe2h86t9G262a4PlKxxeCEE/sUYPwIULi399W6rxPQpiiTj/XadxrpYWmGpFi694sKCiGOmW57z8P/yyw+jQnORMLtNevBrnkwsbMoxB4Xfb3/vDu2UKB0+Yg90jqTPfta8/U0nv7EK6CaZQKBgQDIEVGlNIccSfu6PkoYSBI4Rg3b+t3vdU07EfPDK+gkxKj7ejfPr0xLtpu7Oa++m+rs0e6zTF8NAFDmzMkxyHr/ZZ5ERYm1z7A+PVF1HyItocBvRo6yj9MPjRdq5CygihhiT6EM7jofs/yX4HJKDySxjWzHwlSJoubFpWVPbeDC2QKBgEks+efJLcbokxQLige1/TTcQYViyGRh5rgmtewvqN6LKWtntGd0dDNTEcH2NBhXTSLJPSuWZmfr0Ahhx834dWDQ3xG4oacIk2G6zZoHqoAjCXty3xJvhZBRX+t4hnI2KF7+62VaVKSjrFqzIuC+tJamekIrT0UiLXgOeQsmFCLhAoGAe1CzLS6ujeGDzcbmhmRRK37bZxchX0UwLo2Oon/SBb1JnggQ8C4jnLgF0H5G6Gxw9uoKbQoWLuTbrm1BOdPeb+q1USlgxqKDmaCIrqtV0LGWWLix56KPrvjeOSjLUv7UaizTRkBQpLUOFHw/80jh+74OzQAQCR5FmaJonCdPU4E=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi6VukDnysSVGwV0uaTV/FE5QQXQz4/O1Nernl66EOi1CTKaQDeU1c7P+LDO68yHYsFrN2IjNR7SpO17+NMbDPIJaN8dn8kfMwIs7pHs39OFyz74eY33keGNIwlbe5hIvqD5RAsd75LJqQbkU96E2wex94CjBX49Xvz1BbuTn1glun8UKLK9Koa/D1WA6KlRqVLP7q/xUDwUPYM2f4tL+p8tzIrQBLrGqyqU8+4WlZ/Ng02jyMsD1wvm8P9mBckX0oiYb/M3DP26T/kwNf4id3EtaqI+8kA+haW9CDMLniPUq//8IHvh8UWXwyDoCjr8K1vOE6lcQowxM2Hv3+EXt0QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url ="http://bsi32kuc03.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ "15m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
